package cn.bx.bid.ctrl;

import cn.bx.bid.entity.Page;
import cn.bx.bid.entity.Project;
import cn.bx.bid.service.ProjectService;
import cn.bx.bid.util.AjaxResult;
import cn.bx.bid.util.LogUtil;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;


@Controller
public class ProjectController {
    @Resource
    ProjectService projectService;
    //项目列表
    @GetMapping(value = "projectsSearch",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Page<Project> paging(@RequestParam(name = "page",defaultValue = "1")int pageNo, @RequestParam(name = "limit",defaultValue = "10") int limit, @RequestParam(name = "name",required = false)String name, @RequestParam(name = "category",required = false) String category) {

        Page<Project> p = this.projectService.paging(pageNo,limit,name,category);
        return p;
    }

    //添加项目
    @GetMapping("toProjectAdd")
    public String toAdd() {
        return "projectAdd";
    }

    @PostMapping("projectAdd")
    public String add(HttpSession s, @ModelAttribute("p") Project p, @RequestParam(name = "fujian", required = false) MultipartFile file, HttpServletRequest req) {
        if (!(file == null || file.isEmpty())) {
            //文件后缀 getName()
            String oldName = file.getOriginalFilename();
            String prefix = oldName.substring(oldName.lastIndexOf('.'));
            //新文件名
            String newName = UUID.randomUUID() + prefix;
            //保存到项目的doc目录下
            File path = new File(s.getServletContext().getRealPath("doc"));
            LogUtil.info("文件的路径：" + path.getAbsolutePath());
            if (path.exists() == false)
                path.mkdirs();
            try {
                file.transferTo(new File(path, newName));
                p.setProjectFilePath(newName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        LogUtil.info("新项目的信息时：\n" + p);
        int add = projectService.add(p);
        if(add>0){
            req.setAttribute("msg",add>0?"添加成功":"添加失败");
            return "addSuccess";
        }else{
            return "projectAdd";
        }
    }

//批量删除


    @RequestMapping(value = " ProjectbatchDel",produces = "application/json;charset=utf-8")
    @ResponseBody
    public AjaxResult  ProjectbatchDel(@RequestParam("ids[]") long[] ids) {

        int r = projectService.ProjectbatchDel(ids);
        if (r>0){
            return AjaxResult.success("success");
        }
        return AjaxResult.error("error");
    }
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        // true:允许输入空值，false:不能为空值
    }
}
